package ezXpns;

import java.util.UUID;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FloatField;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.document.Field.Store;

import ezXpns.analysis.Transform;

/**
 * A transform that convert a {@link ezXpns.entry.BudgetEntry} to a Lucene converter
 * 
 * @see Searcher
 */

//@author A0088827L
public class BudgetChangeConverter implements Transform<Pair<UUID, BudgetChange>, Document> {

	public Document apply(Pair<UUID, BudgetChange> pair) {
		UUID id = pair.getLeft();
		BudgetChange change = pair.getRight();
		
		Document doc = new Document();		
		doc.add(new StringField("id", id.toString(), Store.YES));
		doc.add(new TextField("name", change.getName(), Store.NO));
		doc.add(new FloatField("amount", change.getAmount(), Store.NO));
		doc.add(new LongField("date", change.getDate().getTime(), Store.NO));
		doc.add(new StringField("type", change.getType(), Store.NO));
		
		for(Tag tag: change.getAllTags()) {
			if(tag.hasValue()) {
				Float numberValue = tag.getValueAsNumber();
				if(numberValue != null) {
					doc.add(new FloatField(tag.getName(), numberValue, Store.NO));
				}
				else {
					doc.add(new StringField(tag.getName(), tag.getValueAsString(), Store.NO));
				}
			}

			doc.add(new TextField("tags", tag.getName(), Store.NO));
		}		
		return doc;
	}

}
